package com.memory.demo.service;
import com.memory.demo.dao.InimageDao;
import com.memory.demo.entity.Soul;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@Service
public class PaintService {
    private  int count=0;
    @Resource
    private InimageDao inimageDao;

    @Value("${path.path}")
    private String path;

    private static String fontpath="target/classes/static/仿宋_GB2312.ttf";


    public  void  curupian(Soul soul){
        inimageDao.cunrutupian(soul);
    }

    public String tuxiangjiawenzi(String title,String text,String pathh,String imagename){


        String name =title;
        String[] res = text.split("\n");
           path=path+imagename+".jpg";
//        String path="C:\\Users\\69496\\Desktop\\"+imagename+".jpg";

        String [] strr=new String[res.length+1];
        strr[0]=name;
        for (int i=0;i<res.length;i++){
            strr[i+1]=res[i];
        }

        PaintService.createStringMark(pathh,strr,path);

//        Date time=new Date();
//
//        curupian(new Soul(path,name,time));

        return path;

    }


    public static Font loadFont(String fontFileName, float fontSize)  //第一个参数是外部字体名，第二个是字体大小
    {
        try
        {
            File file = new File(fontFileName);
            FileInputStream aixing = new FileInputStream(file);
            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
            Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
            aixing.close();
            return dynamicFontPt;
        }
        catch(Exception e)//异常处理
        {
            e.printStackTrace();
            return new java.awt.Font("宋体", Font.PLAIN, 14);
        }
    }

    /**
     * @param filePath 源图片路径
     * @param outPath  输出图片路径
     * 字体颜色等在函数内部实现的
     */
    //给jpg添加文字
    public static boolean createStringMark(String filePath,String [] str,String outPath)
    {

//        String [] str=new String[5];
//        str[0]="宠爱戒指";
//        str[1]= "女神菲娜的戒指，传说中他说他是猴哥他把";
//        str[2]="";
//        str[3]="";
//        str[4]="然而试试真的如此吗";
//        ArrayList<String> str=new ArrayList<String>();
//        str.set(0, "宠爱戒指");
//        str.set(1,"女神菲娜的戒指，传说中他说他是猴哥他把");
//        str.set(2,"");
//        str.set(3,"");
//        str.set(4,"然而试试真的如此吗");
//        System.out.println(str.size());


        ImageIcon imgIcon=new ImageIcon(filePath);
        Image theImg =imgIcon.getImage();
        int width=theImg.getWidth(null)==-1?200:theImg.getWidth(null);
        int height= theImg.getHeight(null)==-1?200:theImg.getHeight(null);
        System.out.println(width);
        System.out.println(height);
        System.out.println(theImg);
        BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g=bimage.createGraphics();

        Color mycolor =  new Color(150,150,150);
        g.setColor(mycolor);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
        g.setBackground(mycolor);
        g.drawImage(theImg, 0, 0, null );
        g.setFont( loadFont(fontpath, 22f));
//        g.setFont( loadFont("C:\\Users\\69496\\Desktop\\仿宋_GB2312.ttf", 22f));
//        g.setFont(new Font("仿宋2312",Font.PLAIN,27)); //字体、字型、字号

        for(int x=20,y=30,i=0,length=str.length;i<length;i++){
            g.drawString(str[i],x,y); //画文字
            if(i==0){y=384;}
            if(i!=0){y=y+26;}
        }

        //第一行387.5 之后一行25 首行30
        g.dispose();
        try
        {
            FileOutputStream out=new FileOutputStream(outPath); //先用一个特定的输出文件名
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
            param.setQuality(100, true);  //
            encoder.encode(bimage, param);
            out.close();
        }
        catch(Exception e)
        { return false; }
        return true;
    }

}

